package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.utils.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Guanxing
 * @date 2021/9/28
 */
@Component
public class UserLikeManager {

    @Reference
    private UserLikeService userLikeService;

    @Reference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private RecommendUserService recommendUserService;

    @Reference
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //喜欢
    public void saveUserLike(Long userLikeId) {
        //获取当前用户id
        Long loginUserId = UserHolder.getId();
        //保存当前用户的喜欢
        userLikeService.saveUserLike(loginUserId,userLikeId);

        //判断双方是否相互喜欢
        boolean flag = userLikeService.isUserLike(loginUserId,userLikeId);
        if (flag) {
            //添加双方为好友
            friendService.addContacts(loginUserId,userLikeId);

            //添加环信好友
            huanXinTemplate.addContacts(loginUserId,userLikeId);
        }
    }

    //不喜欢
    public void deleteUserLike(Long likeUserId) {
        //获取登录用户id
        Long userId = UserHolder.get().getId();

        //调用userLikeService删除我对它的喜欢
        userLikeService.deleteUserLike(userId, likeUserId);

        //调用friendService删除好友
        friendService.deleteFriend(userId, likeUserId);

        //调用环信删除好友
        huanXinTemplate.deleteContacts(userId, likeUserId);

    }

    //查询我的互相喜欢、喜欢、粉丝数量
    public UserLikeCountVo usersCounts() {
        //获取当前用户id
        Long loginUserId = UserHolder.getId();
        //查询互相喜欢
        Long eachLoveCount = userLikeService.findEachLoveCount(loginUserId);
        //查询我的喜欢
        Long loveCount = userLikeService.findLoveCount(loginUserId);
        //查询粉丝
        Long fanCount = userLikeService.findFanCount(loginUserId);

        UserLikeCountVo userLikeCountVo = new UserLikeCountVo();
        userLikeCountVo.setEachLoveCount(eachLoveCount);
        userLikeCountVo.setLoveCount(loveCount);
        userLikeCountVo.setFanCount(fanCount);

        return userLikeCountVo;


    }

    //查询 互相喜欢，喜欢，粉丝 访客 数据
    public PageBeanVo findUserInfoByType(Integer page, Integer pageSize, String type) {
        //获取当前用户id
        Long loginUserId = UserHolder.getId();

        PageBeanVo pageBeanVo = userLikeService.findUserInfoByType(page,pageSize,type,loginUserId);
        //数据封装
        List<UserLikeVo> items = (List<UserLikeVo>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(items)) {
            for (UserLikeVo userLikeVo : items) {
                //查询用户数据
                UserInfo userInfo = userInfoService.findById(userLikeVo.getId());
                //封装
                BeanUtil.copyProperties(userInfo,userLikeVo);
                //封装缘分值
                RecommendUser recommendUser = recommendUserService.findFateValue(loginUserId, userLikeVo.getId());
                userLikeVo.setMatchRate(recommendUser.getScore().intValue());
            }
        }

        if (StrUtil.equals(type, "4")) {
            //更新一下最后的查询访客时间
            String key = StrUtil.format(ConstantUtil.LAST_ACCESS_TIME, loginUserId);
            stringRedisTemplate.opsForValue().set(key,System.currentTimeMillis() + "");
        }

        return pageBeanVo;
    }
}
